#include "datatemp.h"
#include <QSettings>


#define CONFFILENAME "config.ini"
#define DEFGROUPNAME "setting"
#define  SERVERIP "server_ip"
#define  SERVERPORT  "server_port"
#define  USERNAME  "username"
#define  PASSWORD  "password"
#define  CURRENTDEVICEID  "currentdeviceid"


dataTemp *dataTemp::obj = nullptr;
dataTemp::dataTemp()
{
initData();
}

dataTemp *dataTemp::getObject()
{
   if(obj == nullptr)
       obj = new dataTemp;
   return obj;
}

void dataTemp::saveData()
{
  QSettings set(CONFFILENAME ,QSettings::IniFormat);
  set.beginGroup(DEFGROUPNAME);
  set.setValue(SERVERIP,serverIp);
  set.setValue(SERVERPORT,serverPort);
  set.setValue(USERNAME,username);
  set.setValue(PASSWORD,password);
  set.setValue(CURRENTDEVICEID,currentDeviceId);
  set.endGroup();
}

void dataTemp::initData()
{
    QSettings set(CONFFILENAME, QSettings::IniFormat);
    set.beginGroup(DEFGROUPNAME);
    QStringList keys = set.allKeys();
    if(!keys.contains(SERVERIP))
        set.setValue(SERVERIP,"127.0.0.1");
    serverIp=set.value(SERVERIP).toString();
    if(!keys.contains(SERVERPORT))
        set.setValue(SERVERPORT,10086);
    serverPort = set.value(SERVERPORT).toInt();
    if(!keys.contains(USERNAME))
       set.setValue(USERNAME,"zhangsan");
    username = set.value(USERNAME).toString();
    if(!keys.contains(PASSWORD))
        set.setValue(PASSWORD,"123456");
    password = set.value(PASSWORD).toString();
    if(!keys.contains(CURRENTDEVICEID))
        set.setValue(CURRENTDEVICEID,"D0001");
    currentDeviceId = set.value(CURRENTDEVICEID).toString();
    set.endGroup();
}


